﻿package Interface
{
	import Core.Page;
	import Interface.MenuClasses.UpMenuLabel;
	
	import caurina.transitions.Tweener;	
	
	import flash.display.MovieClip;		
	import flash.text.TextField;	
	import flash.events.MouseEvent;
	
	public class UpMenu extends MovieClip
	{
		private var FMask:MovieClip;
		private var FCurrentPageText:TextField;
		private var FMoneyText:TextField;
		private var FRatingText:TextField;
		private var FBuyButton:MovieClip;
		private var FCurrentPage:Page;
		private var FLabels:Vector.<UpMenuLabel>;
		
		public function UpMenu() 
		{
			FLabels = new Vector.<UpMenuLabel>;
			FCurrentPageText = getChildByName('CurrentPageText') as TextField;
			FMoneyText = getChildByName('MoneyText') as TextField;			
			FMask = getChildByName('MenuMask') as MovieClip;
			FRatingText = getChildByName('RatingText') as TextField;	
			FBuyButton = getChildByName('BuyVoicesButton') as MovieClip;
			FBuyButton.addEventListener(MouseEvent.MOUSE_DOWN, OnBuyButton);
		}
		
		public function FillMenu(APages:Vector.<Page>):void
		{
			var vMenuOffset:Number = new Number();
			for(var i in APages)
			{
				if(APages[i].MenuName != '')
				{
					var vLabel:UpMenuLabel = new UpMenuLabel(APages[i]);
					vLabel.x = 150 + vMenuOffset;
					vLabel.y = 17.5;
					vMenuOffset += vLabel.width;				
					vLabel.addEventListener(MouseEvent.MOUSE_DOWN, OnLabelClick);
					FLabels.push(vLabel);
					addChild(vLabel);					
				}
			}
		}
		
		private function OnLabelClick(e:MouseEvent):void
		{			
			CurrentPage = UpMenuLabel(e.target.parent).PageLink;			
		}
		
		private function OnBuyButton(e:MouseEvent):void
		{
			PagesInterface(parent).GameCore.ShowTransferMessage();
		}
		
		override public function set visible(value:Boolean):void
		{
			var vPreviousState:Boolean = visible;
			super.visible = value;
		}
		
		public function set CurrentPage(APage:Page):void
		{
			if(FCurrentPage != APage)
			{
				FCurrentPage = APage;
				FCurrentPageText.text = FCurrentPage.MenuName;
				for(var i in FLabels)
				{
					if(FLabels[i].PageLink != APage)
						FLabels[i].Selected = false;				
					else
						FLabels[i].Selected = true;
				}				
				
				PagesInterface(parent).GameCore.CurrentPage = FCurrentPage;
			}
		}
		
		public function set Money(AMoney:String):void
		{
			FMoneyText.text = AMoney;
		}
		
		public function set Rating(ARating:String):void
		{
			FRatingText.text = ARating;
		}
	}	
}
